<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
    String xm_id = request.getParameter("xm_id") == null ? "" : request.getParameter("xm_id");
    String xm_name = request.getParameter("xm_name") == null ? "" : request.getParameter("xm_name");
    String xm_code = request.getParameter("xm_code") == null ? "" : request.getParameter("xm_code");
%>
<html>
	<head>
		<title>更新工单集合</title>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
			.tdg{
			padding-top:10px;
			padding-bottom:5px;
			font-size: 12px;
			background-color: #c0ebd7;
			width:20%;
		}
		.tdb{
			padding:5px;
			width:30%;
		}
		</style>
		<script  type="text/javascript"  src="<%=contextPath%>/core/js/datastructs.js"  ></script>
		<script  type="text/javascript"  src="<%=contextPath%>/core/js/sys.js"  ></script>
		<script  type="text/javascript"  src="<%=contextPath%>/core/js/prototype.js"  ></script>
		<script type="text/javascript">
			jQuery(document).ready(function() {
					jQuery('#xmForm').form({
						onLoadSuccess:function(){
						    loadGDSets();
						}
					});
				   jQuery('#xmForm').form('load','<%=contextPath%>/springMVC/xm/getXMGDSets?xm_id=<%=request.getParameter("xm_id")%>');
				

		})
		function loadGDSets(){
			var gdIds = $("gdIds").value;
		    var gdNames = $("gdNames").value;
			rtCallBackGDSet(gdIds,gdNames);
		}	
		function addGDSet(){
			  window.open('<%=contextPath %>/springViews/xm/gdSetSelect.jsp',window,"height="+1000+", width="+800+", top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");

		}
		function rtCallBackGDSet(ids,names){
			  if(ids==""){
				  return;
			  }
			  var keys= new Array(); 
			  var values= new Array(); 
              keys=ids.split(",");
              values=names.split(",");
			    for (i=0;i<keys.length ;i++ )   
			    {   
			        jsAddItemToSelect(document.getElementById("gdset"),values[i],keys[i]);

			    }   
			
		}
		function jsSelectIsExitItem(objSelect, objItemValue) {        
		    var isExit = false;        
		    for (var i = 0; i < objSelect.options.length; i++) {        
		        if (objSelect.options[i].value == objItemValue) {        
		            isExit = true;        
		            break;        
		        }        
		    }        
		    return isExit;        
		}         
   
		// 2.向select选项中 加入一个Item        
		function jsAddItemToSelect(objSelect, objItemText, objItemValue) {        
		    //判断是否存在        
		    if (jsSelectIsExitItem(objSelect, objItemValue)) {        
		       // alert("该Item的Value值已经存在");        
		    } else {        
		        var varItem = new Option(objItemText, objItemValue);      
		        objSelect.options.add(varItem);     
		         
		    }        
		}  
		
		// 4.删除select中选中的项    
		function jsRemoveSelectedItemFromSelect(objSelect) { 
		    var length = objSelect.options.length - 1;    
		    for(var i = length; i >= 0; i--){   
		        if(objSelect[i].selected == true){    
		        	 var hasGds=haveSelect();
						if(hasGds==1){
						alert('项目必须拥有至少一个工单!');
						return false;
						}
		        	if(onSelect(objSelect.options[i].value)){
		            	objSelect.options[i] = null;  
		        	} else{
		        		return false;
		        	}
		        }    
		    }    
		} 
		function clearSel(objSelect){
			objSelect.options.length = 0;  
		}
		 var subSelValues="";
		 var subSelTexts="";
		function getALLSubmitSelItems() { 
			subSelValues="";
			subSelTexts="";
			var objSelect=document.getElementById('gdset');
		         var len=objSelect.options.length;
		    for (var i = 0; i < len; i++) {  
		    	if(i==(len-1))
				{
						subSelValues+=objSelect.options[i].value;
					    subSelTexts+=objSelect.options[i].text;
				}else
				{
					subSelValues+=objSelect.options[i].value+",";
					subSelTexts+=objSelect.options[i].text+",";
					
				}
	       
		    }        
		   
		}
		//判断select 是否为0
		function haveSelect(){
		 var length = document.getElementById('gdset').options.length; 
		 return length;
		}
		function saveGDSets(){
		var hasGds=haveSelect();
			if(hasGds==0){
			alert('项目必须拥有至少一个工单!');
			return;
			}
		    getALLSubmitSelItems();
			 var param ="subSelValues="+subSelValues+"&subSelTexts="+encodeURIComponent(subSelTexts);		
				jQuery.ajax({
				type:'POST',
				url:"<%=contextPath%>/springMVC/xm/updateGdSets?xm_id=<%=xm_id%>&xm_name=<%=xm_name%>&xm_code=<%=xm_code%>",				
				data:param,
				success:function(rtJson){
					if (rtJson.rtState == '0'){
						eval('window.opener.<%=callback%>();');
						window.close();
					}else{
						warningInfo(rtJson.rtMsrg);
					}
				}
			});
		}
		function onSelect(gd_id){
			var param="gd_id="+gd_id+"&xm_id=<%=xm_id%>";
			var flag;
			jQuery.ajax({
					type:'GET',
					url:'<%=contextPath %>/springMVC/xm/getXmHasFinance',
					dataType:"JSON",
					data:param,
					async:false,
					success:function(rtJson){
						if(rtJson.rtState == '0'){
							warningInfo(rtJson.rtMsrg);
							flag=false;
						}else{
							flag=true;
						}
					}
				});
			return flag;
		}
		</script>
	</head>
	<body>
		<form id="xmForm" method="post">
		<div style="width:90%;padding-left:50px;margin-top:20px" align="center">

						
							<table  style="border-collapse: collapse;" border="1px" width="100%" bordercolor="#0eb83a" >
							
								<tr>
								<td align="right" nowrap class="td1">
									包含工单：
									</td>
									<td  nowrap colspan="3">
									<input type="hidden" id="gdIds" name="gdIds">
								    <input type="hidden" id="gdNames" name="gdNames">
									<select name="gdset" class="exchangeSelect" multiple="" id="gdset" style="height:200px;width:100%" ></select>
									<BR/>
										<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:false" onclick="addGDSet(this);">添加工单</a>	
									    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:false" onclick="jsRemoveSelectedItemFromSelect(document.getElementById('gdset'));">移除所选工单</a>	
										
									</td>
						            
								</tr>	
					   		   <tr>
					<td align="right" nowrap class="td1">
					</td>
					<td  nowrap colspan="5">
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="saveGDSets(this);">保存</a>
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
					</td>
				</tr>
				 			</table>	
		</div>
</form>
	</body>
</html>


